#include "main.h"
#include "dac8563control.h"
#include "spi.h"

void dac8563config(uint8_t opcode_address, uint16_t config_reg_data)   //配置寄存器
{
	uint8_t temp[3];
	temp[0] = opcode_address;
	temp[1] = (uint8_t) ((config_reg_data)>>8);
	temp[2] = (uint8_t) (config_reg_data);
	
	HAL_GPIO_WritePin(nss_dac_GPIO_Port,nss_dac_Pin,GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1,temp,3,0xffff);
	HAL_GPIO_WritePin(nss_dac_GPIO_Port,nss_dac_Pin,GPIO_PIN_SET);
	HAL_Delay(1);
}
void wr_dac_x(uint16_t data)     //向通道X写入16位数据并更新 
{
	uint8_t temp[3];
	temp[0] = 0x18;
	temp[1] = (uint8_t) (data >> 8);
	temp[2] = (uint8_t) data;

	HAL_GPIO_WritePin(nss_dac_GPIO_Port,nss_dac_Pin,GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1,temp,3,0xffff);
	HAL_GPIO_WritePin(nss_dac_GPIO_Port,nss_dac_Pin,GPIO_PIN_SET);
}
void wr_dac_y(uint16_t data)     //向通道Y写入16位数据并更新 
{
	uint8_t temp[3];
	temp[0] = 0x19;
	temp[1] = (uint8_t) (data >> 8);
	temp[2] = (uint8_t) data;

	HAL_GPIO_WritePin(nss_dac_GPIO_Port,nss_dac_Pin,GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1,temp,3,0xffff);
	HAL_GPIO_WritePin(nss_dac_GPIO_Port,nss_dac_Pin,GPIO_PIN_SET);
}
void dac_config(void)    //配置寄存器汇总
{
	dac8563config(0x28,0x0001);   //软件复位
	dac8563config(0x20,0x0003);   //两通道上电
	dac8563config(0x38,0x0001);   //使用内部参考电压设置增益为2
	dac8563config(0x18,0x0000);   //向通道x写入数据0x0000并更新
  dac8563config(0x19,0x0010); 	//向通道y写入数据0x0010并更新
}

